﻿Imports System.Data
Imports MyGeneration.dOOdads
Imports ClassSigeco.ClassSigeco

Public Class controller_stock

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property

#Region "Mercaderias"

    Public Function Insert_mercaderia(ByVal conn As String, ByVal mercaderia As String, ByVal stockMinimo As Integer, ByVal idMarca As Integer, ByVal idRubro As Integer, ByVal observaciones As String, ByVal idImpuesto As Integer, ByVal actualizadoPor As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_mercaderia(conn)

            merca.AddNew()
            merca.Mercaderia = mercaderia
            merca.StockMinimo = stockMinimo
            merca.IdMarca = idMarca
            merca.IdRubro = idRubro
            merca.Observaciones = observaciones
            merca.IdImpuesto = idImpuesto
            merca.ActualizadoPor = actualizadoPor
            merca.ActualizadoFecha = DateTime.Now
            merca.Save()

            tx.CommitTransaction()

            Return merca.IdMercaderia

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_mercaderia(ByVal conn As String, ByVal idMercaderia As Integer, _
                                      ByVal mercaderia As String, ByVal stockMinimo As Integer, ByVal idMarca As Integer, ByVal idRubro As Integer, _
                                       ByVal observaciones As String, ByVal idImpuesto As Integer, ByVal actualizadoPor As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_mercaderia(conn)
            merca.Where.IdMercaderia.Value = idMercaderia

            If merca.Query.Load Then

                merca.Mercaderia = mercaderia
                merca.StockMinimo = stockMinimo
                merca.IdMarca = idMarca
                merca.IdRubro = idRubro
                merca.Observaciones = observaciones
                merca.IdImpuesto = idImpuesto
                merca.ActualizadoPor = actualizadoPor
                merca.ActualizadoFecha = DateTime.Now
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_mercaderia(ByVal conn As String, ByVal idMercaderia As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_mercaderia(conn)
            merca.Where.IdMercaderia.Value = idMercaderia

            If merca.Query.Load Then

                merca.MarkAsDeleted()
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_mercaderias_All(ByVal conn As String, ByVal idMercaderia As Integer, ByVal mercaderia As String, _
                                        ByVal idMarca As Integer, ByVal idRubro As Integer) As DataTable
        Try

            Dim vmercas As New vw_mercaderias(conn)

            If idMercaderia > 0 Then
                vmercas.Where.IdMercaderia.Value = idMercaderia
            End If

            If mercaderia <> "" Then
                vmercas.Where.Mercaderia.Value = "%" & mercaderia & "%"
                vmercas.Where.Mercaderia.Operator = WhereParameter.Operand.Like
            End If

            If idMarca > 0 Then
                vmercas.Where.IdMarca.Value = idMarca
            End If

            If idRubro > 0 Then
                vmercas.Where.IdRubro.Value = idRubro
            End If

            vmercas.Query.AddOrderBy("mercaderia", WhereParameter.Dir.ASC)

            If vmercas.Query.Load Then
                If vmercas.DefaultView.Table.Rows.Count > 0 Then
                    Return vmercas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_motivos_stock(ByVal conn As String) As DataTable
        Try

            Dim obj As New si_motivoStock(conn)

            obj.Query.AddOrderBy("Motivo", WhereParameter.Dir.ASC)

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_marcas(ByVal conn As String, ByVal idMarca As Integer, ByVal marca As String) As DataTable
        Try

            Dim obj As New si_marca(conn)

            If idMarca > 0 Then
                obj.Where.IdMarca.Value = idMarca
            End If

            If marca <> "" Then
                obj.Where.Marca.Value = "%" & marca & "%"
                obj.Where.Marca.Operator = WhereParameter.Operand.Like
            End If

            obj.Query.AddOrderBy("Marca", WhereParameter.Dir.ASC)

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_rubros(ByVal conn As String, ByVal idRubro As Integer, ByVal rubro As String) As DataTable
        Try

            Dim obj As New si_rubro(conn)

            If idRubro > 0 Then
                obj.Where.IdRubro.Value = idRubro
            End If

            If rubro <> "" Then
                obj.Where.Rubro.Value = "%" & rubro & "%"
                obj.Where.Rubro.Operator = WhereParameter.Operand.Like
            End If

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_impuestos(ByVal conn As String, ByVal idImpuesto As Integer, ByVal impuesto As String) As DataTable
        Try

            Dim obj As New si_impuesto(conn)

            If idImpuesto > 0 Then
                obj.Where.IdImpuesto.Value = idImpuesto
            End If

            If impuesto <> "" Then
                obj.Where.Impuesto.Value = "%" & impuesto & "%"
                obj.Where.Impuesto.Operator = WhereParameter.Operand.Like
            End If

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function


    Public Function Insert_marcas(ByVal conn As String, ByVal marcaDescrip As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim marca As New si_marca(conn)

            marca.AddNew()
            marca.Marca = marcaDescrip
            marca.Save()

            tx.CommitTransaction()

            Return marca.IdMarca

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_marca(ByVal conn As String, ByVal idMarca As Integer, ByVal marcaDescrip As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_marca(conn)
            merca.Where.IdMarca.Value = idMarca

            If merca.Query.Load Then

                merca.Marca = marcaDescrip
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_marcas(ByVal conn As String, ByVal idMarca As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim marca As New si_marca(conn)
            marca.Where.IdMarca.Value = idMarca

            If marca.Query.Load Then

                marca.MarkAsDeleted()
                marca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function


    Public Function Insert_rubro(ByVal conn As String, ByVal rubroDescrip As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim rubro As New si_rubro(conn)

            rubro.AddNew()
            rubro.Rubro = rubroDescrip
            rubro.Save()

            tx.CommitTransaction()

            Return rubro.IdRubro

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_rubro(ByVal conn As String, ByVal idRubro As Integer, ByVal rubroDescrip As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_rubro(conn)
            merca.Where.IdRubro.Value = idRubro

            If merca.Query.Load Then

                merca.Rubro = rubroDescrip
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_rubro(ByVal conn As String, ByVal idRubro As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim rubro As New si_rubro(conn)
            rubro.Where.IdRubro.Value = idRubro

            If rubro.Query.Load Then

                rubro.MarkAsDeleted()
                rubro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function


    Public Function Insert_impuesto(ByVal conn As String, ByVal impuestoDescrip As String, ByVal valor As Decimal) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim impuesto As New si_impuesto(conn)

            impuesto.AddNew()
            impuesto.Impuesto = impuestoDescrip
            impuesto.Valor = valor
            impuesto.Save()

            tx.CommitTransaction()

            Return impuesto.IdImpuesto

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_impuesto(ByVal conn As String, ByVal idImpuesto As Integer, ByVal impuestoDescrip As String, ByVal valor As Decimal) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_impuesto(conn)
            merca.Where.IdImpuesto.Value = idImpuesto

            If merca.Query.Load Then

                merca.Impuesto = impuestoDescrip
                merca.Valor = valor
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_impuesto(ByVal conn As String, ByVal idImpuesto As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim impuesto As New si_impuesto(conn)
            impuesto.Where.IdImpuesto.Value = idImpuesto

            If impuesto.Query.Load Then

                impuesto.MarkAsDeleted()
                impuesto.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    '------------------------------

    Public Function Insert_deposito(ByVal conn As String, ByVal descripcion As String, ByVal ubicacion As String, ByVal idUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try

            tx.BeginTransaction()

            Dim deposito As New si_deposito(conn)

            deposito.AddNew()
            deposito.Descripcion = descripcion
            deposito.Ubicacion = ubicacion
            deposito.Habilitado = True
            deposito.ActualizadoPor = idUsuario
            deposito.ActualizadoFecha = DateTime.Now
            deposito.Save()

            tx.CommitTransaction()

            Return deposito.IdDeposito

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_deposito(ByVal conn As String, ByVal idDeposito As Integer, _
                                    ByVal descripcion As String, ByVal ubicacion As String, ByVal idUsuario As Integer, ByVal habilitado As Boolean) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New si_deposito(conn)
            deposito.Where.IdDeposito.Value = idDeposito

            If deposito.Query.Load Then
                deposito.Descripcion = descripcion
                deposito.Ubicacion = ubicacion
                deposito.ActualizadoPor = idUsuario
                deposito.Habilitado = habilitado
                deposito.ActualizadoFecha = DateTime.Now
                deposito.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_deposito(ByVal conn As String, ByVal idDeposito As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New si_deposito(conn)
            deposito.Where.IdDeposito.Value = idDeposito

            If deposito.Query.Load Then

                deposito.MarkAsDeleted()
                deposito.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_depositos(ByVal conn As String, ByVal idDeposito As Integer, _
                                  ByVal descripcion As String, ByVal ubicacion As String, ByVal habilitado As Boolean) As DataTable
        Try

            Dim deposito As New vw_depositos(conn)

            If idDeposito > 0 Then
                deposito.Where.IdDeposito.Value = idDeposito
            Else
                deposito.Where.Habilitado.Value = habilitado
            End If

            If descripcion <> "" Then
                deposito.Where.Descripcion.Value = "%" & descripcion & "%"
                deposito.Where.Descripcion.Operator = WhereParameter.Operand.Like
            End If

            If ubicacion <> "" Then
                deposito.Where.Ubicacion.Value = "%" & ubicacion & "%"
                deposito.Where.Ubicacion.Operator = WhereParameter.Operand.Like
            End If

            If deposito.Query.Load Then
                Return deposito.DefaultView.Table
            Else
                Return Nothing
            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

End Class
